package udf.string;

import java.io.IOException;

import org.apache.pig.EvalFunc;

import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;

import util.string.URLCleaner;

public class SimplifyURL extends EvalFunc<Tuple> {

	public Tuple exec(Tuple input) throws IOException {

		if (input.size() != 2 || input.get(0) == null) {
			return null;
		}

		else {

			try {
				Tuple output = TupleFactory.getInstance().newTuple(1);

				String url = (String) input.get(0);
				String level_ = (String) input.get(1);
				int level = Integer.valueOf(level_);
				String domain = URLCleaner.simplifyURL(url, level);

			//	System.out.println("Processing:" + domain + "\t" + level);
				output.set(0, domain);
				return output;

			} catch (Exception e) {
				System.err.println("Failed to process input; error - "
						+ e.getMessage());
				return null;
			}

		}
	}
}